 

 var port = 2004;
 var hostname  = 'localhost';

 //引入内置http模块
 var http = require('http');

 var qs = require('querystring');

 
 var url = require('url');

 var mysql = require('mysql');


//与数据库建立连接
 var con = mysql.createConnection({
     host:'localhost',
     user:'root',
     password:'root',
     database:'pro'
 })

 con.connect();


  //创建HTTP服务，起服务
 var server =  http.createServer((req,res) =>{

    const {pathname,query} = url.parse(req.url);  //格式化请求路径  获取pathname 和query  pathname就是地址栏？问号前面的东西，不包括localhost和端口；query就是地址栏？问号后面的东西

    console.log(pathname);
    // console.log(query);


    res.writeHead(200,{"content-type":"text/html;charset=utf8"});//请求头防止中文乱码
    // res.write("<h2>   boring    </h2>")
    // res.write(pathname);
    // res.write(query);
    //当pathname不等于favicon,ico
    if(pathname != '/favicon.ico'){

        var data = null;
        switch(pathname){
            case '/add':{
                data = {
                    code : 1,
                    msg:'success'

                }
            };break;
            case '/delete':{
                data= {
                    code:1,
                    msg:'success'
                };break;
            }
            
        }
    }




    res.end(JSON.stringify(data));

 }).listen(port,hostname,() =>{
     console.log(`my node server is running at htttp://${hostname}:${port}`);
 })